package com.nit.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nit.store.pojo.dto.output.DistrictDto;
import com.nit.store.pojo.dto.output.OrderDto;
import com.nit.store.pojo.entity.DictDistrict;
import com.nit.store.pojo.entity.Order;
import com.nit.store.pojo.entity.OrderItem;
import com.nit.store.service.OrderService;
import com.nit.store.util.CommonUtils;
import com.nit.store.util.JwtUtils;
import com.nit.store.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author J1ay
 * @Date 2022/7/21 20:08
 * @Description
 */
@RestController
@RequestMapping("/orders")
@Slf4j
public class OrderController {

    @Autowired
    OrderService orderService;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Autowired
    private JwtUtils jwtUtils;

    @PostMapping("/create")
    public JsonResult<Void> create(Long aid, Long[] cids,@RequestHeader(value="Authentication") String token) {
        Long uid = jwtUtils.parseJwtGetId(token.substring(tokenHeader.length()));
        String username = jwtUtils.parseJwtGetName(token.substring(tokenHeader.length()));
        orderService.createOrder(aid, cids, uid, username);
        return JsonResult.ok();
    }

    /**
     * 获取订单信息
     * @return
     */
    @PostMapping("/")
    public JsonResult<List<OrderDto>> getOrderList(int status, @RequestHeader( value="Authentication") String token) {
        Long uid = jwtUtils.parseJwtGetId(token.substring(tokenHeader.length()));
        return JsonResult.ok(orderService.getOrderList(status, uid));
    }

    @PostMapping("/delete")
    public JsonResult<Void> delete(long oid,long pid) {
        orderService.deleteItem(oid, pid);
        return JsonResult.ok();
    }
}
